home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / leim / quail.el.z / quail.el
Encoding:
Text File  |  1998-05-21  |  71.8 KB  |  1,956 lines

  1. ;;; quail.el --- Provides simple input method for multilingual text
  2.  
  3. ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
  4. ;; Licensed to the Free Software Foundation.
  5. ;; Copyright (C) 1997 MORIOKA Tomohiko
  6.  
  7. ;; Author: Kenichi HANDA <handa@etl.go.jp>
  8. ;;       Naoto TAKAHASHI <ntakahas@etl.go.jp>
  9. ;;         modified by MORIOKA Tomohiko <morioka@jaist.ac.jp> for XEmacs
  10. ;; Keywords: mule, multilingual, input method
  11.  
  12. ;; This file is part of GNU Emacs.
  13.  
  14. ;; GNU Emacs is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; GNU Emacs is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ;; GNU General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  26. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  27. ;; Boston, MA 02111-1307, USA.
  28.  
  29. ;;; Commentary:
  30.  
  31. ;; In Quail minor mode, you can input multilingual text easily.  By
  32. ;; defining a translation table (named Quail map) which maps ASCII key
  33. ;; string to multilingual character or string, you can input any text
  34. ;; from ASCII keyboard.
  35. ;;
  36. ;; We use words "translation" and "conversion" differently.  The
  37. ;; former is done by Quail package itself, the latter is the further
  38. ;; process of converting a translated text to some more desirable
  39. ;; text.  For instance, Quail package for Japanese (`quail-jp')
  40. ;; translates Roman text (transliteration of Japanese in Latin
  41. ;; alphabets) to Hiragana text, which is then converted to
  42. ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
  43. ;; CONVERSION-KEYS argument of the Quail package.
  44.  
  45. ;;; Code:
  46.  
  47. (eval-when-compile
  48.   (if (string-match "XEmacs" emacs-version)
  49.       (require 'overlay)
  50.     ))
  51.  
  52. (cond
  53.  ((featurep 'xemacs)
  54.   (require 'overlay)
  55.   )
  56.  (t
  57.   (require 'faces)
  58.   ))
  59.  
  60. ;; Buffer local variables
  61.  
  62. (defvar quail-current-package nil
  63.   "The current Quail package, which depends on the current input method.
  64. See the documentation of `quail-package-alist' for the format.")
  65. (make-variable-buffer-local 'quail-current-package)
  66. (put 'quail-current-package 'permanent-local t)
  67.  
  68. ;; Quail uses the following two buffers to assist users.
  69. ;; A buffer to show available key sequence or translation list.
  70. (defvar quail-guidance-buf nil)
  71. ;; A buffer to show completion list of the current key sequence.
  72. (defvar quail-completion-buf nil)
  73.  
  74. ;; Each buffer in which Quail is activated should use different
  75. ;; guidance buffers.
  76. (make-variable-buffer-local 'quail-guidance-buf)
  77. (put 'quail-guidance-buf 'permanent-local t)
  78.  
  79. ;; A main window showing Quail guidance buffer.
  80. (defvar quail-guidance-win nil)
  81. (make-variable-buffer-local 'quail-guidance-win)
  82.  
  83. (defvar quail-mode nil
  84.   "Non-nil if in Quail minor mode.")
  85. (make-variable-buffer-local 'quail-mode)
  86. (put 'quail-mode 'permanent-local t)
  87.  
  88. (defvar quail-overlay nil
  89.   "Overlay which covers the current translation region of Quail.")
  90. (make-variable-buffer-local 'quail-overlay)
  91.  
  92. (defvar quail-conv-overlay nil
  93.   "Overlay which covers the text to be converted in Quail mode.")
  94. (make-variable-buffer-local 'quail-conv-overlay)
  95.  
  96. (defvar quail-current-key nil
  97.   "Current key for translation in Quail mode.")
  98.  
  99. (defvar quail-current-str nil
  100.   "Currently selected translation of the current key.")
  101.  
  102. (defvar quail-current-translations nil
  103.   "Cons of indices and vector of possible translations of the current key.
  104. Indices is a list of (CURRENT START END BLOCK BLOCKS), where
  105. CURRENT is an index of the current translation,
  106. START and END are indices of the start and end of the current block,
  107. BLOCK is the current block index,
  108. BLOCKS is a number of  blocks of translation.")
  109.  
  110. (defvar quail-current-data nil
  111.   "Any Lisp object holding information of current translation status.
  112. When a key sequence is mapped to TRANS and TRANS is a cons
  113. of actual translation and some Lisp object to be refered
  114. for translating the longer key sequence, this variable is set
  115. to that Lisp object.")
  116. (make-variable-buffer-local 'quail-current-data)
  117.  
  118. ;; A flag to control conversion region.  Normally nil, but if set to
  119. ;; t, it means we must start the new conversion region if new key to
  120. ;; be translated is input.
  121. (defvar quail-reset-conversion-region nil)
  122.  
  123. ;; Quail package handlers.
  124.  
  125. (defvar quail-package-alist nil
  126.   "List of Quail packages.
  127. A Quail package is a list of these elements:
  128.   NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
  129.   FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
  130.   DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
  131.   CONVERSION-KEYS, SIMPLE.
  132.  
  133. QUAIL-MAP is a data structure to map key strings to translations.  For
  134. the format, see the documentation of `quail-map-p'.
  135.  
  136. DECODE-MAP is an alist of translations and corresponding keys.
  137.  
  138. See the documentation of `quail-define-package' for the other elements.")
  139.  
  140. ;; Return various slots in the current quail-package.
  141.  
  142. (defsubst quail-name ()
  143.   "Return the name of the current Quail package."
  144.   (nth 0 quail-current-package))
  145. (defsubst quail-title ()
  146.   "Return the title of the current Quail package."
  147.   (nth 1 quail-current-package))
  148. (defsubst quail-map ()
  149.   "Return the translation map of the current Quail package."
  150.   (nth 2 quail-current-package))
  151. (defsubst quail-guidance ()
  152.   "Return an object used for `guidance' feature of the current Quail package.
  153. See also the documentation of `quail-define-package'."
  154.   (nth 3 quail-current-package))
  155. (defsubst quail-docstring ()
  156.   "Return the documentation string of the current Quail package."
  157.   (nth 4 quail-current-package))
  158. (defsubst quail-translation-keymap ()
  159.   "Return translation keymap in the current Quail package.
  160. Translation keymap is a keymap used while translation region is active."
  161.   (nth 5 quail-current-package))
  162. (defsubst quail-forget-last-selection ()
  163.   "Return `forget-last-selection' flag of the current Quail package.
  164. See also the documentation of `quail-define-package'."
  165.   (nth 6 quail-current-package))
  166. (defsubst quail-deterministic ()
  167.   "Return `deterministic' flag of the current Quail package.
  168. See also the documentation of `quail-define-package'."
  169.   (nth 7 quail-current-package))
  170. (defsubst quail-kbd-translate ()
  171.   "Return `kbd-translate' flag of the current Quail package.
  172. See also the documentation of `quail-define-package'."
  173.   (nth 8 quail-current-package))
  174. (defsubst quail-show-layout ()
  175.   "Return `show-layout' flag of the current Quail package.
  176. See also the documentation of `quail-define-package'."
  177.   (nth 9 quail-current-package))
  178. (defsubst quail-decode-map ()
  179.   "Return decode map of the current Quail package.
  180. It is an alist of translations and corresponding keys."
  181.   (nth 10 quail-current-package))
  182. (defsubst quail-maximum-shortest ()
  183.   "Return `maximum-shortest' flag of the current Quail package.
  184. See also the documentation of `quail-define-package'."
  185.   (nth 11 quail-current-package))
  186. (defsubst quail-overlay-plist ()
  187.   "Return property list of an overly used in the current Quail package."
  188.   (nth 12 quail-current-package))
  189. (defsubst quail-update-translation-function ()
  190.   "Return a function for updating translation in the current Quail package."
  191.   (nth 13 quail-current-package))
  192. (defsubst quail-conversion-keymap ()
  193.   "Return conversion keymap in the current Quail package.
  194. Conversion keymap is a keymap used while conversion region is active
  195.  but translation region is not active."
  196.   (nth 14 quail-current-package))
  197. (defsubst quail-simple ()
  198.   "Return t if the current Quail package is simple."
  199.   (nth 15 quail-current-package))
  200.  
  201. (defsubst quail-package (name)
  202.   "Return Quail package named NAME."
  203.   (assoc name quail-package-alist))
  204.  
  205. (defun quail-add-package (package)
  206.   "Add Quail package PACKAGE to `quail-package-alist'."
  207.   (let ((pac (quail-package (car package))))
  208.     (if pac
  209.     (setcdr pac (cdr package))
  210.       (setq quail-package-alist (cons package quail-package-alist)))))
  211.  
  212. (defun quail-select-package (name)
  213.   "Select Quail package named NAME as the current Quail package."
  214.   (let ((package (quail-package name)))
  215.     (if (null package)
  216.     (error "No Quail package `%s'" name))
  217.     (setq quail-current-package package)
  218.     (setq-default quail-current-package package)
  219.     name))
  220.  
  221. ;;;###autoload
  222. (defun quail-use-package (package-name &rest libraries)
  223.   "Start using Quail package PACKAGE-NAME.
  224. The remaining arguments are libraries to be loaded before using the package."
  225.   (let ((package (quail-package package-name)))
  226.     (if (null package)
  227.     ;; Perhaps we have not yet loaded necessary libraries.
  228.     (while libraries
  229.       (if (not (load (car libraries) t))
  230.           (progn
  231.         (with-output-to-temp-buffer "*Help*"
  232.           (princ "Quail package \"")
  233.           (princ package-name)
  234.           (princ "\" can't be activated\n  because library \"")
  235.           (princ (car libraries))
  236.           (princ "\" is not in `load-path'.
  237.  
  238. The most common case is that you have not yet installed appropriate
  239. libraries in LEIM (Libraries of Emacs Input Method) which is
  240. distributed separately from Emacs.
  241.  
  242. LEIM is available from the same ftp directory as Emacs."))
  243.         (error "Can't use the Quail package `%s'" package-name))
  244.         (setq libraries (cdr libraries))))))
  245.   (quail-select-package package-name)
  246.   (setq current-input-method-title (quail-title))
  247.   (quail-mode 1))
  248.  
  249. (defun quail-inactivate ()
  250.   "Turn off Quail input method."
  251.   (interactive)
  252.   (setq overriding-terminal-local-map nil)
  253.   (quail-mode -1))
  254.  
  255. (or (assq 'quail-mode minor-mode-alist)
  256.     (setq minor-mode-alist
  257.       (cons '(quail-mode " Quail") minor-mode-alist)))
  258.  
  259. (defvar quail-mode-map 
  260.   (let ((map (make-keymap))
  261.     (i ? ))
  262.     (while (< i 127)
  263.       (define-key map (char-to-string i) 'quail-start-translation)
  264.       (setq i (1+ i)))
  265.     (setq i 128)
  266.     (while (< i 256)
  267.       (define-key map (vector (int-char i)) 'quail-start-translation)
  268.       (setq i (1+ i)))
  269.     map)
  270.   "Keymap for Quail mode.")
  271.  
  272. (or (assq 'quail-mode minor-mode-map-alist)
  273.     (setq minor-mode-map-alist
  274.       (cons (cons 'quail-mode quail-mode-map) minor-mode-map-alist)))
  275.  
  276. ;; Since some Emacs Lisp programs (e.g. viper.el) make
  277. ;; minor-mode-map-alist buffer-local, we must be sure to register
  278. ;; quail-mode-map in default-value of minor-mode-map-alist.
  279. (if (local-variable-p 'minor-mode-map-alist nil)
  280.     (let ((map (default-value 'minor-mode-map-alist)))
  281.       (or (assq 'quail-mode map)
  282.       ;; (set-default 'minor-mode-map-alist (cons 'quail-mode map)))))
  283.       (set-default 'minor-mode-map-alist
  284.                (cons (cons 'quail-mode quail-mode-map) map)))))
  285.  
  286. (defvar quail-translation-keymap
  287.   (let ((map (make-keymap))
  288.     (i 0))
  289.     (while (< i ?\ )
  290.       (define-key map (char-to-string i) 'quail-execute-non-quail-command)
  291.       (setq i (1+ i)))
  292.     (while (< i 127)
  293.       (define-key map (char-to-string i) 'quail-self-insert-command)
  294.       (setq i (1+ i)))
  295.     (define-key map "\177" 'quail-delete-last-char)
  296. ;;  (define-key map "\C-\\" 'quail-inactivate)
  297.     (define-key map "\C-f" 'quail-next-translation)
  298.     (define-key map "\C-b" 'quail-prev-translation)
  299.     (define-key map "\C-n" 'quail-next-translation-block)
  300.     (define-key map "\C-p" 'quail-prev-translation-block)
  301.     (define-key map "\C-i" 'quail-completion)
  302.     (define-key map "\C-@" 'quail-select-current)
  303. ;;  (define-key map "\C-c" 'quail-abort-translation)
  304.     (define-key map "\C-h" 'quail-translation-help)
  305. ;;; This interferes with handling of escape sequences on non-X terminals.
  306. ;;;    (define-key map "\e" '(keymap (t . quail-execute-non-quail-command)))
  307.     (define-key map [?\C-\ ] 'quail-select-current)
  308.     (define-key map [tab] 'quail-completion)
  309.     (define-key map [delete] 'quail-delete-last-char)
  310.     (define-key map [backspace] 'quail-delete-last-char)
  311.     (let ((meta-map (make-sparse-keymap)))
  312.       (when (characterp meta-prefix-char)
  313.     (define-key map (char-to-string meta-prefix-char) meta-map))
  314.       (define-key map [escape] meta-map))
  315.     (when (characterp meta-prefix-char)
  316.       (define-key map (vector meta-prefix-char t)
  317.     'quail-execute-non-quail-command))
  318.     ;; At last, define default key binding.
  319.     (set-keymap-default-binding map 'quail-execute-non-quail-command)
  320.     map)
  321.   "Keymap used processing translation in complex Quail modes.
  322. Only a few especially complex input methods use this map;
  323. most use `quail-simple-translation-keymap' instead.
  324. This map is activated while translation region is active.")
  325.  
  326. (defvar quail-simple-translation-keymap
  327.   (let ((map (make-keymap))
  328.     (i 0))
  329.     (while (< i ?\ )
  330.       (define-key map (char-to-string i) 'quail-execute-non-quail-command)
  331.       (setq i (1+ i)))
  332.     (while (< i 127)
  333.       (define-key map (char-to-string i) 'quail-self-insert-command)
  334.       (setq i (1+ i)))
  335.     (define-key map "\177" 'quail-delete-last-char)
  336.     (define-key map [delete] 'quail-delete-last-char)
  337.     (define-key map [backspace] 'quail-delete-last-char)
  338. ;;; This interferes with handling of escape sequences on non-X terminals.
  339. ;;;    (define-key map "\e" '(keymap (t . quail-execute-non-quail-command)))
  340.     (let ((meta-map (make-sparse-keymap)))
  341.       (when (characterp meta-prefix-char)
  342.     (define-key map (char-to-string meta-prefix-char) meta-map))
  343.       (define-key map [escape] meta-map))
  344.     (when (characterp meta-prefix-char)
  345.       (define-key map (vector meta-prefix-char t)
  346.     'quail-execute-non-quail-command))
  347.     ;; At last, define default key binding.
  348.     (set-keymap-default-binding map 'quail-execute-non-quail-command)
  349.     map)
  350.   "Keymap used while processing translation in simple Quail modes.
  351. A few especially complex input methods use `quail--translation-keymap' instead.
  352. This map is activated while translation region is active.")
  353.  
  354. (defvar quail-conversion-keymap
  355.   (let ((map (make-keymap))
  356.     (i 0))
  357.     (while (< i ?\ )
  358.       (define-key map (char-to-string i) 'quail-execute-non-quail-command)
  359.       (setq i (1+ i)))
  360.     (while (< i 127)
  361.       (define-key map (char-to-string i)
  362.     'quail-start-translation-in-conversion-mode)
  363.       (setq i (1+ i)))
  364.     (define-key map "\C-b" 'quail-conversion-backward-char)
  365.     (define-key map "\C-f" 'quail-conversion-forward-char)
  366.     (define-key map "\C-a" 'quail-conversion-beginning-of-region)
  367.     (define-key map "\C-e" 'quail-conversion-end-of-region)
  368.     (define-key map "\C-d" 'quail-conversion-delete-char)
  369.     (define-key map "\C-h" 'quail-conversion-help)
  370. ;;  (define-key map "\C-\\" 'quail-inactivate)
  371. ;;; This interferes with handling of escape sequences on non-X terminals.
  372. ;;;    (define-key map "\e" '(keymap (t . quail-execute-non-quail-command)))
  373.     (define-key map "\177" 'quail-conversion-backward-delete-char)
  374.     (define-key map [delete] 'quail-conversion-backward-delete-char)
  375.     (define-key map [backspace] 'quail-conversion-backward-delete-char)
  376.     (let ((meta-map (make-sparse-keymap)))
  377.       (when (characterp meta-prefix-char)
  378.     (define-key map (char-to-string meta-prefix-char) meta-map))
  379.       (define-key map [escape] meta-map))
  380.     (when (characterp meta-prefix-char)
  381.       (define-key map (vector meta-prefix-char t)
  382.     'quail-execute-non-quail-command))
  383.     ;; At last, define default key binding.
  384.     (set-keymap-default-binding map 'quail-execute-non-quail-command)
  385.     map)
  386.   "Keymap used for processing conversion in Quail mode.
  387. This map is activated while convesion region is active but translation
  388. region is not active.")
  389.  
  390. ;;;###autoload
  391. (defun quail-define-package (name language title
  392.                   &optional guidance docstring translation-keys
  393.                   forget-last-selection deterministic
  394.                   kbd-translate show-layout create-decode-map
  395.                   maximum-shortest overlay-plist
  396.                   update-translation-function
  397.                   conversion-keys simple)
  398.   "Define NAME as a new Quail package for input LANGUAGE.
  399. TITLE is a string to be displayed at mode-line to indicate this package.
  400. Optional arguments are GUIDANCE, DOCSTRING, TRANLSATION-KEYS,
  401.  FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
  402.  CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
  403.  UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
  404.  
  405. GUIDANCE specifies how a guidance string is shown in echo area.
  406. If it is t, list of all possible translations for the current key is shown
  407.  with the currently selected translation being highlighted.
  408. If it is an alist, the element has the form (CHAR . STRING).  Each character
  409.  in the current key is searched in the list and the corresponding string is
  410.  shown.
  411. If it is nil, the current key is shown.
  412.  
  413. DOCSTRING is the documentation string of this package.
  414.  
  415. TRANSLATION-KEYS specifies additional key bindings used while translation
  416. region is active.  It is an alist of single key character vs. corresponding
  417. command to be called.
  418.  
  419. FORGET-LAST-SELECTION non-nil means a selected translation is not kept
  420. for the future to translate the same key.  If this flag is nil, a
  421. translation selected for a key is remembered so that it can be the
  422. first candidate when the same key is entered later.
  423.  
  424. DETERMINISTIC non-nil means the first candidate of translation is
  425. selected automatically without allowing users to select another
  426. translation for a key.  In this case, unselected translations are of
  427. no use for an interactive use of Quail but can be used by some other
  428. programs.  If this flag is non-nil, FORGET-LAST-SELECTION is also set
  429. to t.
  430.  
  431. KBD-TRANSLATE non-nil means input characters are translated from a
  432. user's keyboard layout to the standard keyboard layout.  See the
  433. documentation of `quail-keyboard-layout' and
  434. `quail-keyboard-layout-standard' for more detail.
  435.  
  436. SHOW-LAYOUT non-nil means the `quail-help' command should show
  437. the user's keyboard layout visually with translated characters.
  438. If KBD-TRANSLATE is set, it is desirable to set also this flag unless
  439. this package defines no translations for single character keys.
  440.  
  441. CREATE-DECODE-MAP non-nil means decode map is also created.  A decode
  442. map is an alist of translations and corresponding original keys.
  443. Although this map is not used by Quail itself, it can be used by some
  444. other programs.  For instance, Vietnamese supporting needs this map to
  445. convert Vietnamese text to VIQR format which uses only ASCII
  446. characters to represent Vietnamese characters.
  447.  
  448. MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
  449. length of the shortest sequence.  When we don't have a translation of
  450. key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
  451. the key at \"..AB\" and start translation of \"CD..\".  Hangul
  452. packages, for instance, use this facility.  If this flag is nil, we
  453. break the key just at \"..ABC\" and start translation of \"D..\".
  454.  
  455. OVERLAY-PLIST if non-nil is a property list put on an overlay which
  456. covers Quail translation region.
  457.  
  458. UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
  459. the current translation region accoding to a new translation data.  By
  460. default, a tranlated text or a user's key sequence (if no transltion
  461. for it) is inserted.
  462.  
  463. CONVERSION-KEYS specifies additional key bindings used while
  464. conversion region is active.  It is an alist of single key character
  465. vs. corresponding command to be called.
  466.  
  467. If SIMPLE is non-nil, then we do not alter the meanings of
  468. commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
  469. non-Quail commands."
  470.   (let (translation-keymap conversion-keymap)
  471.     (if deterministic (setq forget-last-selection t))
  472.     (if translation-keys
  473.     (progn
  474.       (setq translation-keymap (copy-keymap
  475.                     (if simple quail-simple-translation-keymap
  476.                       quail-translation-keymap)))
  477.       (while translation-keys
  478.         (define-key translation-keymap
  479.           (car (car translation-keys)) (cdr (car translation-keys)))
  480.         (setq translation-keys (cdr translation-keys))))
  481.       (setq translation-keymap
  482.         (if simple quail-simple-translation-keymap
  483.           quail-translation-keymap)))
  484.     (when conversion-keys
  485.       (setq conversion-keymap (copy-keymap quail-conversion-keymap))
  486.       (while conversion-keys
  487.     (define-key conversion-keymap
  488.       (car (car conversion-keys)) (cdr (car conversion-keys)))
  489.     (setq conversion-keys (cdr conversion-keys))))
  490.     (quail-add-package
  491.      (list name title (list nil) guidance (or docstring "")
  492.        translation-keymap
  493.        forget-last-selection deterministic kbd-translate show-layout
  494.        (if create-decode-map (list 'decode-map) nil)
  495.        maximum-shortest overlay-plist update-translation-function
  496.        conversion-keymap simple))
  497.  
  498.     ;; Update input-method-alist.
  499.     (let ((slot (assoc name input-method-alist))
  500.       (val (list language 'quail-use-package title docstring)))
  501.       (if slot (setcdr slot val)
  502.     (setq input-method-alist (cons (cons name val) input-method-alist)))))
  503.  
  504.   (quail-select-package name))
  505.  
  506. ;; Quail minor mode handlers.
  507.  
  508. ;; Setup overlays used in Quail mode.
  509. (defun quail-setup-overlays (conversion-mode)
  510.   (let ((pos (point)))
  511.     (if (overlayp quail-overlay)
  512.     (move-overlay quail-overlay pos pos)
  513.       (setq quail-overlay (make-overlay pos pos nil nil t))
  514.       (if input-method-highlight-flag
  515.       (overlay-put quail-overlay 'face 'underline))
  516.       (let ((l (quail-overlay-plist)))
  517.     (while l
  518.       (overlay-put quail-overlay (car l) (car (cdr l)))
  519.       (setq l (cdr (cdr l))))))
  520.     (if conversion-mode
  521.     (if (overlayp quail-conv-overlay)
  522.         (if (not (overlay-start quail-conv-overlay))
  523.         (move-overlay quail-conv-overlay pos pos))
  524.       (setq quail-conv-overlay (make-overlay pos pos nil nil t))
  525.       (if input-method-highlight-flag
  526.           (overlay-put quail-conv-overlay 'face 'underline))))))
  527.  
  528. ;; Delete overlays used in Quail mode.
  529. (defun quail-delete-overlays ()
  530.   (if (overlayp quail-overlay)
  531.       (delete-overlay quail-overlay))
  532.   (if (overlayp quail-conv-overlay)
  533.       (delete-overlay quail-conv-overlay)))
  534.  
  535. ;; Kill Quail guidance buffer.  Set in kill-buffer-hook.
  536. (defun quail-kill-guidance-buf ()
  537.   (if (buffer-live-p quail-guidance-buf)
  538.       (kill-buffer quail-guidance-buf)))
  539.  
  540. (defun quail-mode (&optional arg)
  541.   "Toggle Quail minor mode.
  542. With arg, turn Quail mode on if and only if arg is positive.
  543.  
  544. You should not turn on and off Quail mode manually, instead use
  545. the commands `toggle-input-method' or `select-input-methods' (which
  546. see).  They automatically turn on or off this mode.
  547.  
  548. Try \\[describe-bindings] in Quail mode to see the available key bindings.
  549. The command \\[describe-input-method] describes the current Quail package."
  550.   (setq quail-mode
  551.     (if (null arg) (null quail-mode)
  552.       (> (prefix-numeric-value arg) 0)))
  553.   (if (null quail-mode)
  554.       ;; Let's turn off Quail mode.
  555.       (progn
  556.     (quail-hide-guidance-buf)
  557.     (quail-delete-overlays)
  558.     (setq describe-current-input-method-function nil)
  559.     (run-hooks 'quail-mode-exit-hook))
  560.     ;; Let's turn on Quail mode.
  561.     ;; At first, be sure that quail-mode is at the first element of
  562.     ;; minor-mode-map-alist.
  563.     ;; The following code removed by slb because it corrupts the XEmacs
  564.     ;; minor-mode-map-alist
  565. ;    (or (eq (car minor-mode-map-alist) 'quail-mode)
  566. ;    (let ((l minor-mode-map-alist))
  567. ;      (while (cdr l)
  568. ;        (if (eq (car (cdr l)) 'quail-mode)
  569. ;        (progn
  570. ;          (setcdr l (cdr (cdr l)))
  571. ;          (setq l nil))
  572. ;          (setq l (cdr l))))
  573. ;      (setq minor-mode-map-alist (cons 'quail-mode minor-mode-map-alist))))
  574.     ;; End bogus code removal.
  575.     (if (null quail-current-package)
  576.     ;; Quail package is not yet selected.  Select one now.
  577.     (let (name)
  578.       (if quail-package-alist
  579.           (setq name (car (car quail-package-alist)))
  580.         (setq quail-mode nil)
  581.         (error "No Quail package loaded"))
  582.       (quail-select-package name)))
  583.     (setq inactivate-current-input-method-function 'quail-inactivate)
  584.     (setq describe-current-input-method-function 'quail-help)
  585.     (quail-delete-overlays)
  586.     (quail-show-guidance-buf)
  587.     ;; If we are in minibuffer, turn off the current input method
  588.     ;; before exiting.
  589.     (if (eq (selected-window) (minibuffer-window))
  590.     (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
  591.     (make-local-hook 'post-command-hook)
  592.     (make-local-hook 'kill-buffer-hook)
  593.     (add-hook 'kill-buffer-hook 'quail-kill-guidance-buf nil t)
  594.     (run-hooks 'quail-mode-hook))
  595.   (force-mode-line-update))
  596.  
  597. (defun quail-exit-from-minibuffer ()
  598.   (inactivate-input-method)
  599.   (if (<= (minibuffer-depth) 1)
  600.       (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
  601.  
  602. (defvar quail-saved-current-map nil)
  603. (defvar quail-saved-current-buffer nil)
  604.  
  605. ;; Toggle Quail mode.  This function is added to `post-command-hook'
  606. ;; in Quail mode, to turn Quail mode temporarily off, or back on after
  607. ;; one non-Quail command.
  608. (defun quail-toggle-mode-temporarily ()
  609.   (if quail-mode
  610.       ;; We are going to handle following events out of Quail mode.
  611.       (setq quail-saved-current-buffer (current-buffer)
  612.         quail-saved-current-map overriding-terminal-local-map
  613.         quail-mode nil
  614.         overriding-terminal-local-map nil)
  615.     ;; We have just executed one non-Quail command.  We don't need
  616.     ;; this hook any more.
  617.     (remove-hook 'post-command-hook 'quail-toggle-mode-temporarily t)
  618.     (if (eq (current-buffer) quail-saved-current-buffer)
  619.     ;; We should go back to Quail mode only when the current input
  620.     ;; method was not turned off by the last command.
  621.     (when current-input-method
  622.       (setq quail-mode t
  623.         overriding-terminal-local-map quail-saved-current-map)
  624.       (if input-method-exit-on-invalid-key
  625.           (inactivate-input-method)))
  626.       ;; The last command changed the current buffer, we should not go
  627.       ;; back to Quail mode in this new buffer, but should turn on
  628.       ;; Quail mode in the original buffer.
  629.       (save-excursion
  630.     (set-buffer quail-saved-current-buffer)
  631.     (setq quail-mode t)
  632.     (quail-delete-overlays)))))
  633.  
  634. (defun quail-execute-non-quail-command ()
  635.   "Execute one non-Quail command out of Quail mode.
  636. The current translation and conversion are terminated."
  637.   (interactive)
  638.   (let* ((key (this-command-keys))
  639.      (keylist (listify-key-sequence key)))
  640.     (setq unread-command-events
  641.       (append keylist unread-command-events)))
  642.   (reset-this-command-lengths)
  643.   (quail-terminate-translation)
  644.   (quail-delete-overlays)
  645.   (setq overriding-terminal-local-map nil)
  646.   (if (buffer-live-p quail-guidance-buf)
  647.       (save-excursion
  648.     (set-buffer quail-guidance-buf)
  649.     (erase-buffer)))
  650.   (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t))
  651.  
  652. ;; Keyboard layout translation handlers.
  653.  
  654. ;; Some Quail packages provide localized keyboard simulation which
  655. ;; requires a particular keyboard layout.  In this case, what we need
  656. ;; is locations of keys the user entered, not character codes
  657. ;; generated by those keys.  However, for the moment, there's no
  658. ;; common way to get such information.  So, we ask a user to give
  659. ;; information of his own keyboard layout, then translate it to the
  660. ;; standard layout which we defined so that all Quail packages depend
  661. ;; just on it.
  662.  
  663. (defconst quail-keyboard-layout-standard
  664.   "\
  665.                               \
  666.   1!2@3#4$5%6^7&8*9(0)-_=+`~  \
  667.   qQwWeErRtTyYuUiIoOpP[{]}    \
  668.   aAsSdDfFgGhHjJkKlL;:'\"\\|    \
  669.   zZxXcCvVbBnNmM,<.>/?        \
  670.                               "
  671.   "Standard keyboard layout of printable characters Quail assumes.
  672. See the documentation of `quail-keyboard-layout' for this format.
  673. This layout is almost the same as that of VT100,
  674.  but the location of key \\ (backslash) is just right of key ' (single-quote),
  675.  not right of RETURN key.")
  676.  
  677. (defvar quail-keyboard-layout quail-keyboard-layout-standard
  678.   "A string which represents physical key layout of a particular keyboard.
  679. We assume there are six rows and each row has 15 keys (columns),
  680.     the first row is above the `1' - `0' row,
  681.     the first column of the second row is left of key `1',
  682.     the first column of the third row is left of key `q',
  683.     the first column of the fourth row is left of key `a',
  684.     the first column of the fifth row is left of key `z',
  685.     the sixth row is below the `z' - `/' row.
  686. Nth (N is even) and (N+1)th characters in the string are non-shifted
  687.  and shifted characters respectively at the same location.
  688. The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
  689. The command `quail-set-keyboard-layout' usually sets this variable.")
  690.  
  691. (defconst quail-keyboard-layout-len 180)
  692.  
  693. ;; Here we provide several examples of famous keyboard layouts.
  694.  
  695. (defvar quail-keyboard-layout-alist
  696.   (list
  697.    '("sun-type3" . "\
  698.                               \
  699.   1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
  700.   qQwWeErRtTyYuUiIoOpP[{]}    \
  701.   aAsSdDfFgGhHjJkKlL;:'\"      \
  702.   zZxXcCvVbBnNmM,<.>/?        \
  703.                               ")
  704.    '("atari-german" . "\
  705.                               \
  706.   1!2\"3\2474$5%6&7/8(9)0=\337?'`#^  \
  707.   qQwWeErRtTzZuUiIoOpP\374\334+*    \
  708.   aAsSdDfFgGhHjJkKlL\366\326\344\304~|    \
  709. <>yYxXcCvVbBnNmM,;.:-_        \
  710.                               ")
  711.    (cons "standard" quail-keyboard-layout-standard))
  712.   "Alist of keyboard names and corresponding layout strings.
  713. See the documentation of `quail-keyboard-layout' for the format of
  714.  the layout string.")
  715.  
  716. ;;;###autoload
  717. (defun quail-set-keyboard-layout (kbd-type)
  718.   "Set the current keyboard layout to the same as keyboard KBD-TYPE.
  719.  
  720. Since some Quail packages depends on a physical layout of keys (not
  721. characters generated by them), those are created by assuming the
  722. standard layout defined in `quail-keyboard-layout-standard'.  This
  723. function tells Quail system the layout of your keyboard so that what
  724. you type is correctly handled."
  725.   (interactive
  726.    (let* ((completing-ignore-case t)
  727.       (type (completing-read "Keyboard type: "
  728.                  quail-keyboard-layout-alist)))
  729.      (list type)))
  730.   (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
  731.     (if (null layout)
  732.     ;; Here, we had better ask a user to define his own keyboard
  733.     ;; layout interactively.
  734.     (error "Unknown keyboard type `%s'" kbd-type))
  735.     (setq quail-keyboard-layout (cdr layout))))
  736.  
  737. (defun quail-keyboard-translate (ch)
  738.   "Translate CHAR according to `quail-keyboard-layout' and return the result."
  739.   (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
  740.       ;; All Quail packages are designed based on
  741.       ;; `quail-keyboard-layout-standard'.
  742.       ch
  743.     (let ((i 0))
  744.       (while (and (< i quail-keyboard-layout-len)
  745.           (/= ch (aref quail-keyboard-layout i)))
  746.     (setq i (1+ i)))
  747.       (if (= i quail-keyboard-layout-len)
  748.       ;; CH is not in quail-keyboard-layout, which means that a
  749.       ;; user typed a key which generated a character code to be
  750.       ;; handled out of Quail.  Just return CH and make
  751.       ;; quail-execute-non-quail-command handle it correctly.
  752.       ch
  753.     (let ((char (aref quail-keyboard-layout-standard i)))
  754.       (if (= char ?\ )
  755.           ;; A user typed a key at the location not convered by
  756.           ;; quail-keyboard-layout-standard.  Just return CH as
  757.           ;; well as above.
  758.           ch
  759.         char))))))
  760.  
  761. ;; Quail map
  762.  
  763. (defsubst quail-map-p (object)
  764.   "Return t if OBJECT is a Quail map.
  765.  
  766. A Quail map holds information how a particular key should be translated.
  767. Its format is (TRANSLATION . ALIST).
  768. TRANSLATION is either a character, or a cons (INDEX . VECTOR).
  769. In the latter case, each element of VECTOR is a candidate for the translation,
  770. and INDEX points the currently selected translation.
  771.  
  772. ALIST is normally a list of elements that look like (CHAR . DEFN),
  773. where DEFN is another Quail map for a longer key (CHAR added to the
  774. current key).  It may also be a symbol of a function which returns an
  775. alist of the above format.
  776.  
  777. Just after a Quail package is read, TRANSLATION may be a string or a
  778. vector.  Then each element of the string or vector is a candidate for
  779. the translation.  These objects are transformed to cons cells in the
  780. format \(INDEX . VECTOR), as described above."
  781.   (and (consp object)
  782.        (let ((translation (car object)))
  783.      (or (characterp translation) (null translation)
  784.          (vectorp translation) (stringp translation)
  785.          (symbolp translation)
  786.          (and (consp translation) (not (vectorp (cdr translation))))))
  787.        (let ((alist (cdr object)))
  788.      (or (and (listp alist) (consp (car alist)))
  789.          (symbolp alist)))))
  790.  
  791. ;;;###autoload
  792. (defmacro quail-define-rules (&rest rules)
  793.   "Define translation rules of the current Quail package.
  794. Each argument is a list of KEY and TRANSLATION.
  795. KEY is a string meaning a sequence of keystrokes to be translated.
  796. TRANSLATION is a character, a string, a vector, a Quail map, or a function.
  797. If it is a character, it is the sole translation of KEY.
  798. If it is a string, each character is a candidate for the translation.
  799. If it is a vector, each element (string or character) is a candidate
  800.   for the translation.
  801. In these cases, a key specific Quail map is generated and assigned to KEY.
  802.  
  803. If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
  804.  it is used to handle KEY."
  805.   `(quail-install-map
  806.     ',(let ((l rules)
  807.         (map (list nil)))
  808.     (while l
  809.       (quail-defrule-internal (car (car l)) (car (cdr (car l))) map)
  810.       (setq l (cdr l)))
  811.     map)))
  812.  
  813. ;;;###autoload
  814. (defun quail-install-map (map)
  815.   "Install the Quail map MAP in the current Quail package.
  816. The installed map can be referred by the function `quail-map'."
  817.   (if (null quail-current-package)
  818.       (error "No current Quail package"))
  819.   (if (null (quail-map-p map))
  820.       (error "Invalid Quail map `%s'" map))
  821.   (setcar (cdr (cdr quail-current-package)) map))
  822.  
  823. ;;;###autoload
  824. (defun quail-defrule (key translation &optional name)
  825.   "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
  826. KEY is a string meaning a sequence of keystrokes to be translated.
  827. TRANSLATION is a character, a string, a vector, a Quail map,
  828.  a function, or a cons.
  829. It it is a character, it is the sole translation of KEY.
  830. If it is a string, each character is a candidate for the translation.
  831. If it is a vector, each element (string or character) is a candidate
  832.  for the translation.
  833. If it is a cons, the car is one of the above and the cdr is a function
  834.  to call when translating KEY (the return value is assigned to the
  835.  variable `quail-current-data').  If the cdr part is not a function,
  836.  the value itself is assigned to `quail-current-data'.
  837. In these cases, a key specific Quail map is generated and assigned to KEY.
  838.  
  839. If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
  840.  it is used to handle KEY.
  841. Optional argument NAME, if specified, says which Quail package
  842. to define this translation rule in.  The default is to define it in the
  843. current Quail package."
  844.   (if name
  845.       (let ((package (quail-package name)))
  846.     (if (null package)
  847.         (error "No Quail package `%s'" name))
  848.     (setq quail-current-package package)))
  849.   (quail-defrule-internal key translation (quail-map)))
  850.  
  851. ;;;###autoload
  852. (defun quail-defrule-internal (key trans map)
  853.   "Define KEY as TRANS in a Quail map MAP."
  854.   (if (null (stringp key))
  855.       "Invalid Quail key `%s'" key)
  856.   ;; 1997/5/26 by MORIOKA Tomohiko
  857.   ;;    modified for XEmacs
  858.   (if (not (or (characterp trans) (stringp trans) (vectorp trans)
  859.            (consp trans)
  860.            (symbolp trans)
  861.            (quail-map-p trans)))
  862.       (error "Invalid Quail translation `%s'" trans))
  863.   (if (null (quail-map-p map))
  864.       (error "Invalid Quail map `%s'" map))
  865.   (let ((len (length key))
  866.     (idx 0)
  867.     ch entry)
  868.     ;; Make a map for registering TRANS if necessary.
  869.     (while (< idx len)
  870.       (if (null (consp map))
  871.       ;; We come here, for example, when we try to define a rule
  872.       ;; for "ABC" but a rule for "AB" is already defined as a
  873.       ;; symbol.
  874.       (error "Quail key %s is too long" key))
  875.       (setq ch (aref key idx)
  876.         entry (assq ch (cdr map)))
  877.       (if (null entry)
  878.       (progn
  879.         (setq entry (cons ch (list nil)))
  880.         (setcdr map (cons entry (cdr map)))))
  881.       (setq map (cdr entry))
  882.       (setq idx (1+ idx)))
  883.     (if (symbolp trans)
  884.     (if (cdr map)
  885.         ;; We come here, for example, when we try to define a rule
  886.         ;; for "AB" as a symbol but a rule for "ABC" is already
  887.         ;; defined.
  888.         (error "Quail key %s is too short" key)
  889.       (setcdr entry trans))
  890.       (if (quail-map-p trans)
  891.       (if (not (listp (cdr map)))
  892.           ;; We come here, for example, when we try to define a rule
  893.           ;; for "AB" as a symbol but a rule for "ABC" is already
  894.           ;; defined.
  895.           (error "Quail key %s is too short" key)
  896.         (if (not (listp (cdr trans)))
  897.         (if (cdr map)
  898.             ;; We come here, for example, when we try to
  899.             ;; define a rule for "AB" as a symbol but a rule
  900.             ;; for "ABC" is already defined.
  901.             (error "Quail key %s is too short" key)
  902.           (setcdr entry trans))
  903.           (setcdr entry (append trans (cdr map)))))
  904.     (setcar map trans)))))
  905.  
  906. (defun quail-get-translation (def key len)
  907.   "Return the translation specified as DEF for KEY of length LEN.
  908. The translation is either a character or a cons of the form (INDEX . VECTOR),
  909. where VECTOR is a vector of candidates (character or string) for
  910. the translation, and INDEX points into VECTOR to specify the currently
  911. selected translation."
  912.   (if (and def (symbolp def))
  913.       ;; DEF is a symbol of a function which returns valid translation.
  914.       (setq def (funcall def key len)))
  915.   (if (and (consp def) (not (vectorp (cdr def))))
  916.       (setq def (car def)))
  917.  
  918.   (cond
  919.    ((or (characterp def) (consp def))
  920.     def)
  921.  
  922.    ((null def)
  923.     ;; No translation.
  924.     nil)
  925.  
  926.    ((stringp def)
  927.     ;; Each character in DEF is a candidate of translation.  Reform
  928.     ;; it as (INDICES . VECTOR).
  929.     (setq def (string-to-vector def))
  930.     ;; But if the length is 1, we don't need vector but a single
  931.     ;; candidate as the translation.
  932.     (if (= (length def) 1)
  933.     (aref def 0)
  934.       (cons (list 0 0 0 0 nil) def)))
  935.  
  936.    ((vectorp def)
  937.     ;; Each element (string or character) in DEF is a candidate of
  938.     ;; translation.  Reform it as (INDICES . VECTOR).
  939.     (cons (list 0 0 0 0 nil) def))
  940.  
  941.    (t
  942.     (error "Invalid object in Quail map: %s" def))))
  943.  
  944. (defun quail-lookup-key (key &optional len)
  945.   "Lookup KEY of length LEN in the current Quail map and return the definition.
  946. The returned value is a Quail map specific to KEY."
  947.   (or len
  948.       (setq len (length key)))
  949.   (let ((idx 0)
  950.     (map (quail-map))
  951.     (kbd-translate (quail-kbd-translate))
  952.     slot ch translation def)
  953.     (while (and map (< idx len))
  954.       (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
  955.          (aref key idx)))
  956.       (setq idx (1+ idx))
  957.       (if (and (cdr map) (symbolp (cdr map)))
  958.       (setcdr map (funcall (cdr map) key idx)))
  959.       (setq slot (assq ch (cdr map)))
  960.       (if (and (cdr slot) (symbolp (cdr slot)))
  961.       (setcdr slot (funcall (cdr slot) key idx)))
  962.       (setq map (cdr slot)))
  963.     (setq def (car map))
  964.     (setq quail-current-translations nil)
  965.     (if (and map (setq translation (quail-get-translation def key len)))
  966.     (progn
  967.       (if (and (consp def) (not (vectorp (cdr def))))
  968.           (progn
  969.         (if (not (equal (car def) translation))
  970.             ;; We must reflect TRANSLATION to car part of DEF.
  971.             (setcar def translation))
  972.         (setq quail-current-data
  973.               (if (functionp (cdr def))
  974.               (funcall (cdr def))
  975.             (cdr def))))
  976.         (if (not (equal def translation))
  977.         ;; We must reflect TRANSLATION to car part of MAP.
  978.         (setcar map translation)))
  979.       (if (and (consp translation) (vectorp (cdr translation))) 
  980.           (progn
  981.         (setq quail-current-translations translation)
  982.         (if (quail-forget-last-selection)
  983.             (setcar (car quail-current-translations) 0))))
  984.       ;; We may have to reform cdr part of MAP.
  985.       (if (and (cdr map) (symbolp (cdr map)))
  986.           (progn
  987.         (setcdr map (funcall (cdr map) key len))))
  988.       ))
  989.     map))
  990.  
  991. ;; If set to non-nil, exit conversion mode before starting new translation.
  992. (defvar quail-exit-conversion-mode nil)
  993.  
  994. (defvar quail-prefix-arg nil)
  995.  
  996. (defun quail-start-translation (arg)
  997.   "Start translating the typed character in Quail mode."
  998.   (interactive "*_P")
  999.   (setq prefix-arg arg)
  1000.   (setq quail-prefix-arg arg)
  1001.   (setq unread-command-events
  1002.     (cons last-command-event unread-command-events))
  1003.   ;; Check the possibility of translating the last key.
  1004.   (if (and (characterp (event-to-character last-command-event))
  1005.        (assq (if (quail-kbd-translate)
  1006.              (quail-keyboard-translate
  1007.               (event-to-character last-command-event))
  1008.            (event-to-character last-command-event))
  1009.          (cdr (quail-map))))
  1010.       ;; Ok, we can start translation.
  1011.       (if (quail-conversion-keymap)
  1012.       ;; We must start translation in conversion mode.
  1013.       (setq quail-exit-conversion-mode nil
  1014.         overriding-terminal-local-map (quail-conversion-keymap))
  1015.     (quail-setup-overlays nil)
  1016.     (setq quail-current-key "")
  1017.     (setq overriding-terminal-local-map (quail-translation-keymap)))
  1018.     ;; Since the last event doesn't start any translation, handle it
  1019.     ;; out of Quail mode.  We come back to Quail mode later by setting
  1020.     ;; function `quail-toggle-mode-temporarily' in
  1021.     ;; `post-command-hook'.
  1022.     (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
  1023.  
  1024. (defsubst quail-point-in-conversion-region ()
  1025.   "Return non-nil value if the point is in conversion region of Quail mode."
  1026.   (let (start pos)
  1027.     (and (setq start (overlay-start quail-conv-overlay))
  1028.      (>= (setq pos (point)) start)
  1029.      (<= pos (overlay-end quail-conv-overlay)))))
  1030.  
  1031. (defun quail-start-translation-in-conversion-mode ()
  1032.   "Start translating the typed character in conversion mode of Quail mode."
  1033.   (interactive "*")
  1034.   (setq unread-command-events
  1035.     (cons last-command-event unread-command-events))
  1036.   ;; Check the possibility of translating the last key.
  1037.   (if (and (characterp (event-to-character last-command-event))
  1038.        (assq (if (quail-kbd-translate)
  1039.              (quail-keyboard-translate
  1040.               (event-to-character last-command-event))
  1041.            (event-to-character last-command-event))
  1042.          (cdr (quail-map))))
  1043.       ;; Ok, we can start translation.
  1044.       (progn
  1045.     (quail-setup-overlays t)
  1046.     (setq quail-current-key "")
  1047.     (setq overriding-terminal-local-map (quail-translation-keymap))
  1048.     (move-overlay quail-overlay (point) (point)))
  1049.     ;; Since the last event doesn't start any translation, handle it
  1050.     ;; out of Quail mode.  We come back to Quail mode later by setting
  1051.     ;; function `quail-toggle-mode-temporarily' in
  1052.     ;; `post-command-hook'.
  1053.     (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
  1054.  
  1055. (defsubst quail-delete-region ()
  1056.   "Delete the text in the current translation region of Quail."
  1057.   (if (overlay-start quail-overlay)
  1058.       (delete-region (overlay-start quail-overlay)
  1059.              (overlay-end quail-overlay))))
  1060.  
  1061. (defun quail-terminate-translation ()
  1062.   "Terminate the translation of the current key."
  1063.   (when (overlayp quail-overlay)
  1064.     (let ((start (overlay-start quail-overlay)))
  1065.       (if (and start
  1066.            (< start (overlay-end quail-overlay)))
  1067.       ;; Here we simulate self-insert-command.
  1068.       (let ((seq (string-to-sequence
  1069.               (buffer-substring (overlay-start quail-overlay)
  1070.                     (overlay-end quail-overlay))
  1071.               'list))
  1072.         last-command-char)
  1073.         (goto-char start)
  1074.         (quail-delete-region)
  1075.         (setq last-command-char (car seq))
  1076.         (self-insert-command (or quail-prefix-arg 1))
  1077.         (setq quail-prefix-arg nil)
  1078.         (setq seq (cdr seq))
  1079.         (while seq
  1080.           (setq last-command-char (car seq))
  1081.           (self-insert-command 1)
  1082.           (setq seq (cdr seq))))))
  1083.     (delete-overlay quail-overlay))
  1084.   (if (buffer-live-p quail-guidance-buf)
  1085.       (save-excursion
  1086.     (set-buffer quail-guidance-buf)
  1087.     (erase-buffer)))
  1088.   (setq overriding-terminal-local-map
  1089.     (quail-conversion-keymap))
  1090.   ;; Run this hook only when the current input method doesn't require
  1091.   ;; conversion.  When conversion is required, the conversion function
  1092.   ;; should run this hook at a proper timing.
  1093.   (unless (quail-conversion-keymap)
  1094.     (run-hooks 'input-method-after-insert-chunk-hook)))
  1095.  
  1096. (defun quail-select-current ()
  1097.   "Select the current text shown in Quail translation region."
  1098.   (interactive)
  1099.   (quail-terminate-translation))
  1100.  
  1101. ;; Update the current translation status according to CONTROL-FLAG.
  1102. ;; If CONTROL-FLAG is integer value, it is the number of keys in the
  1103. ;; head quail-current-key which can be translated.  The remaining keys
  1104. ;; are put back to unread-command-events to be handled again.
  1105. ;; If CONTROL-FLAG is t, terminate the translation for the whole keys
  1106. ;; in quail-current-key.
  1107. ;; If CONTROL-FLAG is nil, proceed the translation with more keys.
  1108.  
  1109. (defun quail-update-translation (control-flag)
  1110.   ;; 1997/9/26 Comment outed by MORIOKA Tomohiko
  1111.   ;;    `overlay' emulation of XEmacs can not represent 0 length region
  1112.   ;;(quail-delete-region)
  1113.   (let ((func (quail-update-translation-function)))
  1114.     (if func
  1115.     (funcall func control-flag)
  1116.       (let ((start (overlay-start quail-overlay))
  1117.         (end (overlay-end quail-overlay)))
  1118.     (if (numberp control-flag)
  1119.         (let ((len (length quail-current-key)))
  1120.           (while (> len control-flag)
  1121.         (setq len (1- len))
  1122.         (setq unread-command-events
  1123.               ;; 1997/5/26 by MORIOKA Tomohiko
  1124.               ;;    modified for XEmacs
  1125.               (cons (character-to-event (aref quail-current-key len))
  1126.                 unread-command-events)))
  1127.           (insert (or quail-current-str
  1128.               (substring quail-current-key 0 len)))
  1129.           )
  1130.       (insert (or quail-current-str quail-current-key))
  1131.       )
  1132.     (if (and start end)
  1133.         (delete-region start end)
  1134.       ))))
  1135.   (quail-update-guidance)
  1136.   (if control-flag
  1137.       (quail-terminate-translation)))
  1138.  
  1139. (defun quail-self-insert-command ()
  1140.   "Add the typed character to the key for translation."
  1141.   (interactive "*")
  1142.   ;; 1997/5/26 by MORIOKA Tomohiko <morioka@jaist.ac.jp>
  1143.   ;;    modified for XEmacs
  1144.   (setq quail-current-key
  1145.     (concat quail-current-key (char-to-string
  1146.                    (event-to-character last-command-event))))
  1147.   (unless (catch 'quail-tag
  1148.         (quail-update-translation (quail-translate-key))
  1149.         t)
  1150.     ;; If someone throws for `quail-tag' by value nil, we exit from
  1151.     ;; translation mode.
  1152.     (setq overriding-terminal-local-map nil)))
  1153.  
  1154. ;; Return the actual definition part of Quail map MAP.
  1155. (defun quail-map-definition (map)
  1156.   (let ((def (car map)))
  1157.     (if (and (consp def) (not (vectorp (cdr def))))
  1158.     (setq def (car def)))
  1159.     def))
  1160.  
  1161. ;; Return a string to be shown as the current translation of key
  1162. ;; sequence of length LEN.  DEF is a definition part of Quail map for
  1163. ;; the sequence.
  1164. (defun quail-get-current-str (len def)
  1165.   (or (and (consp def) (aref (cdr def) (car (car def))))
  1166.       def
  1167.       (and (> len 1)
  1168.        (let ((str (quail-get-current-str
  1169.                (1- len)
  1170.                (quail-map-definition (quail-lookup-key
  1171.                           quail-current-key (1- len))))))
  1172.          (if str
  1173.          (concat (if (stringp str) str (char-to-string str))
  1174.              (substring quail-current-key (1- len) len)))))))
  1175.  
  1176. (defvar quail-guidance-translations-starting-column 20)
  1177.  
  1178. ;; Update `quail-current-translations' to make RELATIVE-INDEX the
  1179. ;; current translation.
  1180. (defun quail-update-current-translations (&optional relative-index)
  1181.   (let* ((indices (car quail-current-translations))
  1182.      (cur (car indices))
  1183.      (start (nth 1 indices))
  1184.      (end (nth 2 indices)))
  1185.     ;; Validate the index number of current translation.
  1186.     (if (< cur 0)
  1187.     (setcar indices (setq cur 0))
  1188.       (if (>= cur (length (cdr quail-current-translations)))
  1189.       (setcar indices
  1190.           (setq cur (1- (length (cdr quail-current-translations)))))))
  1191.  
  1192.     (if (or (null end)            ; We have not yet calculated END.
  1193.         (< cur start)        ; We moved to the previous block.
  1194.         (>= cur end))        ; We moved to the next block.
  1195.     (let ((len (length (cdr quail-current-translations)))
  1196.           (maxcol (- (window-width quail-guidance-win)
  1197.              quail-guidance-translations-starting-column))
  1198.           (block (nth 3 indices))
  1199.           col idx width trans num-items blocks)
  1200.       (if (< cur start)
  1201.           ;; We must calculate from the head.
  1202.           (setq start 0 block 0)
  1203.         (if end            ; i.e. (>= cur end)
  1204.         (setq start end)))
  1205.       (setq idx start col 0 end start num-items 0)
  1206.       ;; Loop until we hit the tail, or reach the block of CUR.
  1207.       (while (and (< idx len) (>= cur end))
  1208.         (if (= num-items 0)
  1209.         (setq start idx col 0 block (1+ block)))
  1210.         (setq trans (aref (cdr quail-current-translations) idx))
  1211.         (setq width (if (characterp trans) (char-width trans)
  1212.               (string-width trans)))
  1213.         (setq col (+ col width 3) num-items (1+ num-items))
  1214.         (if (and (> num-items 0)
  1215.              (or (>= col maxcol) (> num-items 10)))
  1216.         (setq end idx num-items 0)
  1217.           (setq idx (1+ idx))))
  1218.       (setcar (nthcdr 3 indices) block)
  1219.       (if (>= idx len)
  1220.           (progn
  1221.         ;; We hit the tail before reaching MAXCOL.
  1222.         (setq end idx)
  1223.         (setcar (nthcdr 4 indices) block)))
  1224.       (setcar (cdr indices) start)
  1225.       (setcar (nthcdr 2 indices) end)))
  1226.     (if relative-index
  1227.     (if (>= (+ start relative-index) end)
  1228.         (setcar indices end)
  1229.       (setcar indices (+ start relative-index))))
  1230.     (setq quail-current-str
  1231.       (aref (cdr quail-current-translations) (car indices)))))
  1232.  
  1233. (defun quail-translate-key ()
  1234.   "Translate the current key sequence according to the current Quail map.
  1235. Return t if we can terminate the translation.
  1236. Return nil if the current key sequence may be followed by more keys.
  1237. Return number if we can't find any translation for the current key
  1238. sequence.  The number is the count of valid keys in the current
  1239. sequence counting from the head."
  1240.   (let* ((len (length quail-current-key))
  1241.      (map (quail-lookup-key quail-current-key len))
  1242.      def ch)
  1243.     (if map
  1244.     (let ((def (quail-map-definition map)))
  1245.       (setq quail-current-str (quail-get-current-str len def))
  1246.       ;; Return t only if we can terminate the current translation.
  1247.       (and
  1248.        ;; No alternative translations.
  1249.        (or (null (consp def)) (= (length (cdr def)) 1))
  1250.        ;; No translation for the longer key.
  1251.        (null (cdr map))
  1252.        ;; No shorter breaking point.
  1253.        (or (null (quail-maximum-shortest))
  1254.            (< len 3)
  1255.            (null (quail-lookup-key quail-current-key (1- len)))
  1256.            (null (quail-lookup-key
  1257.               (substring quail-current-key -2 -1) 1)))))
  1258.  
  1259.       ;; There's no translation for the current key sequence.  Before
  1260.       ;; giving up, we must check two possibilities.
  1261.       (cond ((and
  1262.           (quail-maximum-shortest)
  1263.           (>= len 4)
  1264.           (setq def (quail-map-definition
  1265.              (quail-lookup-key quail-current-key (- len 2))))
  1266.           (quail-lookup-key (substring quail-current-key -2) 2))
  1267.          ;; Now the sequence is "...ABCD", which can be split into
  1268.          ;; "...AB" and "CD..." to get valid translation.
  1269.          ;; At first, get translation of "...AB".
  1270.          (setq quail-current-str (quail-get-current-str (- len 2) def))
  1271.          ;; Then, return the length of "...AB".
  1272.          (- len 2))
  1273.  
  1274.         ((and (> len 0)
  1275.           (quail-lookup-key (substring quail-current-key 0 -1))
  1276.           quail-current-translations
  1277.           (not (quail-deterministic))
  1278.           (setq ch (aref quail-current-key (1- len)))
  1279.           (>= ch ?0) (<= ch ?9))
  1280.          ;; A numeric key is entered to select a desirable translation.
  1281.          (setq quail-current-key (substring quail-current-key 0 -1))
  1282.          ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
  1283.          (setq ch (if (= ch ?0) 9 (- ch ?1)))
  1284.          (quail-update-current-translations ch)
  1285.          ;; And, we can terminate the current translation.
  1286.          t)
  1287.  
  1288.         (t
  1289.          ;; No way to handle the last character in this context.
  1290.          (1- len))))))
  1291.  
  1292. (defun quail-next-translation ()
  1293.   "Select next translation in the current batch of candidates."
  1294.   (interactive)
  1295.   (if quail-current-translations
  1296.       (let ((indices (car quail-current-translations)))
  1297.     (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
  1298.         ;; We are already at the tail.
  1299.         (beep)
  1300.       (setcar indices (1+ (car indices)))
  1301.       (quail-update-current-translations)
  1302.       (quail-update-translation nil)))
  1303.     (quail-execute-non-quail-command)))
  1304.  
  1305. (defun quail-prev-translation ()
  1306.   "Select previous translation in the current batch of candidates."
  1307.   (interactive)
  1308.   (if quail-current-translations
  1309.       (let ((indices (car quail-current-translations)))
  1310.     (if (= (car indices) 0)
  1311.         ;; We are already at the head.
  1312.         (beep)
  1313.       (setcar indices (1- (car indices)))
  1314.       (quail-update-current-translations)
  1315.       (quail-update-translation nil)))
  1316.     (quail-execute-non-quail-command)))
  1317.  
  1318. (defun quail-next-translation-block ()
  1319.   "Select from the next block of translations."
  1320.   (interactive)
  1321.   (if quail-current-translations
  1322.       (let* ((indices (car quail-current-translations))
  1323.          (offset (- (car indices) (nth 1 indices))))
  1324.     (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
  1325.         ;; We are already at the last block.
  1326.         (beep)
  1327.       (setcar indices (+ (nth 2 indices) offset))
  1328.       (quail-update-current-translations)
  1329.       (quail-update-translation nil)))
  1330.     (quail-execute-non-quail-command)))
  1331.  
  1332. (defun quail-prev-translation-block ()
  1333.   "Select the previous batch of 10 translation candidates."
  1334.   (interactive)
  1335.   (if quail-current-translations
  1336.       (let* ((indices (car quail-current-translations))
  1337.          (offset (- (car indices) (nth 1 indices))))
  1338.     (if (= (nth 1 indices) 0)
  1339.         ;; We are already at the first block.
  1340.         (beep)
  1341.       (setcar indices (1- (nth 1 indices)))
  1342.       (quail-update-current-translations)
  1343.       (if (< (+ (nth 1 indices) offset) (nth 2 indices))
  1344.           (progn
  1345.         (setcar indices (+ (nth 1 indices) offset))
  1346.         (quail-update-current-translations)))
  1347.       (quail-update-translation nil)))
  1348.     (quail-execute-non-quail-command)))
  1349.  
  1350. (defun quail-abort-translation ()
  1351.   "Abort translation and delete the current Quail key sequence."
  1352.   (interactive)
  1353.   (quail-delete-region)
  1354.   (quail-terminate-translation))
  1355.  
  1356. (defun quail-delete-last-char ()
  1357.   "Delete the last input character from the current Quail key sequence."
  1358.   (interactive)
  1359.   (if (= (length quail-current-key) 1)
  1360.       (quail-abort-translation)
  1361.     (setq quail-current-key (substring quail-current-key 0 -1))
  1362.     (quail-update-translation (quail-translate-key))))
  1363.  
  1364. ;; For conversion mode.
  1365.  
  1366. (defun quail-conversion-backward-char ()
  1367.   (interactive)
  1368.   (if (<= (point) (overlay-start quail-conv-overlay))
  1369.       (error "Beginning of conversion region"))
  1370.   (forward-char -1))
  1371.  
  1372. (defun quail-conversion-forward-char ()
  1373.   (interactive)
  1374.   (if (>= (point) (overlay-end quail-conv-overlay))
  1375.       (error "End of conversion region"))
  1376.   (forward-char 1))
  1377.  
  1378. (defun quail-conversion-beginning-of-region ()
  1379.   (interactive)
  1380.   (goto-char (overlay-start quail-conv-overlay)))
  1381.  
  1382. (defun quail-conversion-end-of-region ()
  1383.   (interactive)
  1384.   (goto-char (overlay-end quail-conv-overlay)))
  1385.  
  1386. (defun quail-conversion-delete-char ()
  1387.   (interactive)
  1388.   (if (>= (point) (overlay-end quail-conv-overlay))
  1389.       (error "End of conversion region"))
  1390.   (delete-char 1)
  1391.   (when (= (overlay-start quail-conv-overlay)
  1392.        (overlay-end quail-conv-overlay))
  1393.     (quail-delete-overlays)
  1394.     (setq overriding-terminal-local-map nil)))
  1395.  
  1396. (defun quail-conversion-backward-delete-char ()
  1397.   (interactive)
  1398.   (if (<= (point) (overlay-start quail-conv-overlay))
  1399.       (error "Beginning of conversion region"))
  1400.   (delete-char -1)
  1401.   (when (= (overlay-start quail-conv-overlay)
  1402.        (overlay-end quail-conv-overlay))
  1403.     (quail-delete-overlays)
  1404.     (setq overriding-terminal-local-map nil)))
  1405.  
  1406. (defun quail-do-conversion (func &rest args)
  1407.   "Call FUNC to convert text in the current conversion region of Quail.
  1408. Remaining args are for FUNC."
  1409.   (delete-overlay quail-overlay)
  1410.   (apply func args))
  1411.  
  1412. (defun quail-no-conversion ()
  1413.   "Do no conversion of the current conversion region of Quail."
  1414.   (interactive)
  1415.   (quail-delete-overlays)
  1416.   (setq overriding-terminal-local-map nil)
  1417.   (run-hooks 'input-method-after-insert-chunk-hook))
  1418.  
  1419. ;; Guidance, Completion, and Help buffer handlers.
  1420.  
  1421. ;; Make a new one-line frame for Quail guidance buffer.
  1422. (defun quail-make-guidance-frame (buf)
  1423.   (let* ((fparam (frame-parameters))
  1424.      (top (cdr (assq 'top fparam)))
  1425.      (border (cdr (assq 'border-width fparam)))
  1426.      (internal-border (cdr (assq 'internal-border-width fparam)))
  1427.      (newtop (- top
  1428.             (frame-char-height) (* internal-border 2) (* border 2))))
  1429.     (if (< newtop 0)
  1430.     (setq newtop (+ top (frame-pixel-height))))
  1431.     (let* ((frame (make-frame (append '((user-position . t) (height . 1)
  1432.                     (minibuffer) (menu-bar-lines . 0))
  1433.                       (cons (cons 'top newtop) fparam))))
  1434.        (win (frame-first-window frame)))
  1435.       (set-window-buffer win buf)
  1436.       ;;(set-window-dedicated-p win t)
  1437.       )))
  1438.  
  1439. ;; Setup Quail completion buffer.
  1440. (defun quail-setup-completion-buf ()
  1441.   (unless (buffer-live-p quail-completion-buf)
  1442.     (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
  1443.     (save-excursion
  1444.       (set-buffer quail-completion-buf)
  1445.       (setq quail-overlay (make-overlay 1 1))
  1446.       (overlay-put quail-overlay 'face 'highlight))))
  1447.  
  1448. ;; Return t iff the current Quail package requires showing guidance
  1449. ;; buffer.
  1450. (defun quail-require-guidance-buf ()
  1451.   (and input-method-verbose-flag
  1452.        (not (and (eq (selected-window) (minibuffer-window))
  1453.          (quail-simple)))))
  1454.  
  1455. (defun quail-show-guidance-buf ()
  1456.   "Display a guidance buffer for Quail input method in some window.
  1457. Create the buffer if it does not exist yet.
  1458. The buffer is normally displayed at the echo area,
  1459. but if the current buffer is a minibuffer, it is shown in
  1460. the bottom-most ordinary window of the same frame,
  1461. or in a newly created frame (if the selected frame has no other windows)."
  1462.   (when (quail-require-guidance-buf)
  1463.     ;; At first, setup a guidance buffer.
  1464.     (or (buffer-live-p quail-guidance-buf)
  1465.     (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*")))
  1466.     (let ((title (quail-title)))
  1467.       (save-excursion
  1468.     (set-buffer quail-guidance-buf)
  1469.     ;; To show the title of Quail package.
  1470.     (setq current-input-method t
  1471.           current-input-method-title title)
  1472.     (erase-buffer)
  1473.     (or (overlayp quail-overlay)
  1474.         (progn
  1475.           (setq quail-overlay (make-overlay 1 1))
  1476.           (overlay-put quail-overlay 'face 'highlight)))
  1477.     (delete-overlay quail-overlay)
  1478.     (set-buffer-modified-p nil)))
  1479.     (bury-buffer quail-guidance-buf)
  1480.  
  1481.     ;; Then, display it in an appropriate window.
  1482.     (let ((win (minibuffer-window)))
  1483.       (if (eq (selected-window) win)
  1484.       ;; Since we are in minibuffer, we can't use it for guidance.
  1485.       (if (eq win (frame-root-window))
  1486.           ;; Create a frame.  It is sure that we are using some
  1487.           ;; window system.
  1488.           (quail-make-guidance-frame quail-guidance-buf)
  1489.         ;; Find the bottom window and split it if necessary.
  1490.         (let (height)
  1491.           (setq win (window-at 0 (- (frame-height) 2)))
  1492.           (setq height (window-height win))
  1493.           ;; If WIN is tall enough, split it vertically and use
  1494.           ;; the lower one.
  1495.           (if (>= height 4)
  1496.           (let ((window-min-height 2))
  1497.             ;; Here, `split-window' returns a lower window
  1498.             ;; which is what we wanted.
  1499.             (setq win (split-window win (- height 2)))))
  1500.           (set-window-buffer win quail-guidance-buf)
  1501.           ;;(set-window-dedicated-p win t)
  1502.           ))
  1503.     (set-window-buffer win quail-guidance-buf))
  1504.       (setq quail-guidance-win win)))
  1505.  
  1506.   ;; And, create a buffer for completion.
  1507.   (quail-setup-completion-buf)
  1508.   (bury-buffer quail-completion-buf))
  1509.  
  1510. (defun quail-hide-guidance-buf ()
  1511.   "Hide the Quail guidance buffer."
  1512.   (if (buffer-live-p quail-guidance-buf)
  1513.       (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
  1514.         win)
  1515.     (while win-list
  1516.       (setq win (car win-list) win-list (cdr win-list))
  1517.       (if (window-minibuffer-p win)
  1518.           ;; We are using echo area for the guidance buffer.
  1519.           ;; Vacate it to the deepest minibuffer.
  1520.           (set-window-buffer win
  1521.                  (format " *Minibuf-%d*" (minibuffer-depth)))
  1522.         (if (eq win (frame-root-window (window-frame win)))
  1523.         (progn
  1524.           ;; We are using a separate frame for guidance buffer.
  1525.           ;;(set-window-dedicated-p win nil)
  1526.           (delete-frame (window-frame win)))
  1527.           ;;(set-window-dedicated-p win nil)
  1528.           (delete-window win)))))))
  1529.  
  1530. (defun quail-update-guidance ()
  1531.   "Update the Quail guidance buffer and completion buffer (if displayed now)."
  1532.   ;; Update guidance buffer.
  1533.   (if (quail-require-guidance-buf)
  1534.       (let ((guidance (quail-guidance)))
  1535.     (cond ((or (eq guidance t)
  1536.            (listp guidance))
  1537.            ;; Show the current possible translations.
  1538.            (quail-show-translations))
  1539.           ((null guidance)
  1540.            ;; Show the current input keys.
  1541.            (let ((key quail-current-key))
  1542.          (save-excursion
  1543.            (set-buffer quail-guidance-buf)
  1544.            (erase-buffer)
  1545.            (insert key)))))))
  1546.  
  1547.   ;; Update completion buffer if displayed now.  We highlight the
  1548.   ;; selected candidate string in *Completion* buffer if any.
  1549.   (let ((win (get-buffer-window quail-completion-buf))
  1550.     key str pos)
  1551.     (if win
  1552.     (save-excursion
  1553.       (setq str (if (stringp quail-current-str)
  1554.             quail-current-str
  1555.               (if (characterp quail-current-str)
  1556.               (char-to-string quail-current-str)))
  1557.         key quail-current-key)
  1558.       (set-buffer quail-completion-buf)
  1559.       (goto-char (point-min))
  1560.       (if (null (search-forward (concat " " key ":") nil t))
  1561.           (delete-overlay quail-overlay)
  1562.         (setq pos (point))
  1563.         (if (and str (search-forward (concat "." str) nil t))
  1564.           (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
  1565.         (move-overlay quail-overlay (match-beginning 0) (point)))
  1566.         ;; Now POS points end of KEY and (point) points end of STR.
  1567.         (if (pos-visible-in-window-p (point) win)
  1568.         ;; STR is already visible.
  1569.         nil
  1570.           ;; We want to make both KEY and STR visible, but if the
  1571.           ;; window is too short, make at least STR visible.
  1572.           (setq pos (progn (point) (goto-char pos)))
  1573.           (beginning-of-line)
  1574.           (set-window-start win (point))
  1575.           (if (not (pos-visible-in-window-p pos win))
  1576.           (set-window-start win pos))
  1577.           ))))))
  1578.  
  1579. (defun quail-show-translations ()
  1580.   "Show the current possible translations."
  1581.   (let* ((key quail-current-key)
  1582.      (map (quail-lookup-key quail-current-key)))
  1583.     (if quail-current-translations
  1584.     (quail-update-current-translations))
  1585.     (save-excursion
  1586.       (set-buffer quail-guidance-buf)
  1587.       (erase-buffer)
  1588.  
  1589.       ;; Show the current key.
  1590.       (let ((guidance (quail-guidance)))
  1591.     (if (listp guidance)
  1592.         ;; We must show the specified PROMPTKEY instead of the
  1593.         ;; actual typed keys.
  1594.         (let ((i 0)
  1595.           (len (length key))
  1596.           prompt-key)
  1597.           (while (< i len)
  1598.         (setq prompt-key (cdr (assoc (aref key i) guidance)))
  1599.         (insert (or prompt-key (aref key i)))
  1600.         (setq i (1+ i))))
  1601.       (insert key)))
  1602.  
  1603.       ;; Show followable keys.
  1604.       (if (cdr map)
  1605.       (let ((l (cdr map)))
  1606.         (insert "[")
  1607.         (while l
  1608.           (insert (car (car l)))
  1609.           (setq l (cdr l)))
  1610.         (insert "]")))
  1611.  
  1612.       ;; Show list of translations.
  1613.       (if quail-current-translations
  1614.       (let* ((indices (car quail-current-translations))
  1615.          (cur (car indices))
  1616.          (start (nth 1 indices))
  1617.          (end (nth 2 indices))
  1618.          (idx start))
  1619.         (indent-to (- quail-guidance-translations-starting-column 7))
  1620.         (insert (format "(%02d/"(nth 3 indices))
  1621.             (if (nth 4 indices)
  1622.             (format "%02d)" (nth 4 indices))
  1623.               "??)"))
  1624.         (while (< idx end)
  1625.           (insert (format " %d." (if (= (- idx start) 9) 0
  1626.                        (1+ (- idx start)))))
  1627.           (let ((pos (point)))
  1628.         (insert (aref (cdr quail-current-translations) idx))
  1629.         (if (= idx cur)
  1630.             (move-overlay quail-overlay pos (point))))
  1631.           (setq idx (1+ idx)))))
  1632.       )))
  1633.  
  1634. (defun quail-completion ()
  1635.   "List all completions for the current key.
  1636. All possible translations of the current key and whole possible longer keys
  1637.  are shown."
  1638.   (interactive)
  1639.   (quail-setup-completion-buf)
  1640.   (let ((key quail-current-key)
  1641.     (map (quail-lookup-key quail-current-key)))
  1642.     (save-excursion
  1643.       (set-buffer quail-completion-buf)
  1644.       (erase-buffer)
  1645.       (insert "Possible completion and corresponding translations are:\n")
  1646.       (quail-completion-1 key map 1)
  1647.       (goto-char (point-min))
  1648.       (display-buffer (current-buffer)))
  1649.       (quail-update-guidance)))
  1650.  
  1651. ;; List all completions of KEY in MAP with indentation INDENT.
  1652. (defun quail-completion-1 (key map indent)
  1653.   (let ((len (length key)))
  1654.     (indent-to indent)
  1655.     (insert key ":")
  1656.     (if (and (symbolp map) (fboundp map))
  1657.     (setq map (funcall map key len)))
  1658.     (if (car map)
  1659.     (quail-completion-list-translations map key (+ indent len 1))
  1660.       (insert " -\n"))
  1661.     (setq indent (+ indent 2))
  1662.     (if (cdr map)
  1663.     (let ((l (cdr map))
  1664.           (newkey (make-string (1+ len) 0))
  1665.           (i 0))
  1666.       ;; Set KEY in the first LEN characters of NEWKEY.
  1667.       (while (< i len)
  1668.         (aset newkey i (aref key i))
  1669.         (setq i (1+ i)))
  1670.       (while l            ; L = ((CHAR . DEFN) ....) ;
  1671.         (aset newkey len (car (car l)))
  1672.         (quail-completion-1 newkey (cdr (car l)) indent)
  1673.         (setq l (cdr l)))))))
  1674.  
  1675. ;; List all possible translations of KEY in Quail map MAP with
  1676. ;; indentation INDENT.
  1677. (defun quail-completion-list-translations (map key indent)
  1678.   (let ((translations
  1679.      (quail-get-translation (car map) key (length key))))
  1680.     (if (characterp translations)
  1681.     (insert "(1/1) 1." translations "\n")
  1682.       ;; We need only vector part.
  1683.       (setq translations (cdr translations))
  1684.       ;; Insert every 10 elements with indices in a line.
  1685.       (let ((len (length translations))
  1686.         (i 0)
  1687.         num)
  1688.     (while (< i len)
  1689.       (when (zerop (% i 10))
  1690.         (when (>= i 10)
  1691.           (newline)
  1692.           (indent-to indent))
  1693.         (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
  1694.       ;; We show the last digit of FROM while converting
  1695.       ;; 0,1,..,9 to 1,2,..,0.
  1696.       (insert (format " %d." (% (1+ i) 10)))
  1697.       (insert (aref translations i))
  1698.       (setq i (1+ i)))
  1699.     (newline)))))
  1700.  
  1701. (defun quail-help ()
  1702.   "Show brief description of the current Quail package."
  1703.   (interactive)
  1704.   (let ((package quail-current-package))
  1705.     (with-output-to-temp-buffer "*Quail-Help*"
  1706.       (save-excursion
  1707.     (set-buffer standard-output)
  1708.     (let ((quail-current-package package))
  1709.       (insert "Quail input method (name:"
  1710.           (quail-name)
  1711.           ", mode line indicator:["
  1712.           (quail-title)
  1713.           "])\n---- Documentation ----\n"
  1714.           (quail-docstring))
  1715.       (newline)
  1716.       (if (quail-show-layout) (quail-show-kbd-layout))
  1717.       (quail-help-insert-keymap-description
  1718.        quail-mode-map
  1719.        "---- Key bindings (before starting translation) ----
  1720. key        binding
  1721. ---        -------\n")
  1722.       (quail-help-insert-keymap-description
  1723.        (quail-translation-keymap)
  1724.        "--- Key bindings (while translating) ---
  1725. key        binding
  1726. ---        -------\n")
  1727.       (if (quail-conversion-keymap)
  1728.           (quail-help-insert-keymap-description
  1729.            (quail-conversion-keymap)
  1730.            "--- Key bindings (while converting) ---
  1731. key        binding
  1732. ---        -------\n"))
  1733.       (help-mode))))))
  1734.  
  1735. (defun quail-help-insert-keymap-description (keymap &optional header)
  1736.   (let (from to)
  1737.     (if header
  1738.     (insert header))
  1739.     (save-excursion
  1740.       (save-window-excursion
  1741.     (let ((overriding-terminal-local-map keymap))
  1742.       (describe-bindings))
  1743.     (set-buffer "*Help*")
  1744.     (goto-char (point-min))
  1745.     (forward-line 4)
  1746.     (setq from (point))
  1747.     (search-forward "Global Bindings:" nil 'move)
  1748.     (beginning-of-line)
  1749.     (setq to (point))))
  1750.     (insert-buffer-substring "*Help*" from to)))
  1751.  
  1752. (defun quail-show-kbd-layout ()
  1753.   "Show keyboard layout with key tops of multilingual characters."
  1754.   (insert "--- Keyboard layout ---\n")
  1755.   (let* ((i 0) ch)
  1756.     (while (< i quail-keyboard-layout-len)
  1757.       (if (= (% i 30) 0)
  1758.       (progn
  1759.         (newline)
  1760.         (indent-to (/ i 30)))
  1761.     (if (= (% i 2) 0)
  1762.         (insert "   ")))
  1763.       (setq ch (aref quail-keyboard-layout i))
  1764.       (when (and (quail-kbd-translate)
  1765.          (/= ch ?\ ))
  1766.     ;; This is the case that the current input method simulates
  1767.     ;; some keyboard layout (which means it requires keyboard
  1768.     ;; translation) and a key at location `i' exists on users
  1769.     ;; keyboard.  We must translate that key by
  1770.     ;; `quail-keyboard-layout-standard'.  But if if there's no
  1771.     ;; corresponding key in that standard layout, we must simulate
  1772.     ;; what is inserted if that key is pressed by setting CH a
  1773.     ;; minus value.
  1774.     (setq ch (aref quail-keyboard-layout-standard i))
  1775.     (if (= ch ?\ )
  1776.         (setq ch (- (aref quail-keyboard-layout i)))))
  1777.       (if (< ch 0)
  1778.       (let ((last-command-event (character-to-event (- ch))))
  1779.         (self-insert-command 1))
  1780.     (if (= ch ?\ )
  1781.         (insert ch)
  1782.       (let* ((map (cdr (assq ch (cdr (quail-map)))))
  1783.          (translation (and map (quail-get-translation 
  1784.                     (car map) (char-to-string ch) 1))))
  1785.         (if (characterp translation)
  1786.         (insert translation)
  1787.           (if (consp translation)
  1788.           (insert (aref (cdr translation) (car translation)))
  1789.         (let ((last-command-event (character-to-event ch)))
  1790.           (self-insert-command 1)))))))
  1791.       (setq i (1+ i))))
  1792.   (newline))
  1793.  
  1794. (defun quail-translation-help ()
  1795.   "Show help message while translating in Quail mode."
  1796.   (interactive)
  1797.   (let ((package quail-current-package)
  1798.     (current-key quail-current-key))
  1799.     (with-output-to-temp-buffer "*Quail-Help*"
  1800.       (save-excursion
  1801.     (set-buffer standard-output)
  1802.     (let ((quail-current-package package))
  1803.       (princ "You are translating the key sequence ")
  1804.       (prin1 quail-current-key)
  1805.       (princ" in Quail mode.\n")
  1806.       (quail-help-insert-keymap-description
  1807.        (quail-translation-keymap)
  1808.        "-----------------------
  1809. key        binding
  1810. ---        -------\n"))
  1811.     (help-mode)))))
  1812.  
  1813. (defun quail-conversion-help ()
  1814.   "Show help message while converting in Quail mode."
  1815.   (interactive)
  1816.   (let ((package quail-current-package)
  1817.     (str (buffer-substring (overlay-start quail-conv-overlay)
  1818.                    (overlay-end quail-conv-overlay))))
  1819.     (with-output-to-temp-buffer "*Quail-Help*"
  1820.       (save-excursion
  1821.     (set-buffer standard-output)
  1822.     (let ((quail-current-package package))
  1823.       (princ "You are converting the string ")
  1824.       (prin1 str)
  1825.       (princ " in Quail mode.\n")
  1826.       (quail-help-insert-keymap-description
  1827.        (quail-conversion-keymap)
  1828.        "-----------------------
  1829. key        binding
  1830. ---        -------\n"))
  1831.     (help-mode)))))
  1832.  
  1833.  
  1834. (defvar quail-directory-name "quail"
  1835.   "Name of Quail directory which cotains Quail packages.
  1836. This is a sub-directory of LEIM directory.")
  1837.  
  1838. ;;;###autoload
  1839. (defun quail-update-leim-list-file (dirname &rest dirnames)
  1840.   "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
  1841. DIRNAME is a directory containing Emacs input methods;
  1842. normally, it should specify the `leim' subdirectory
  1843. of the Emacs source tree.
  1844.  
  1845. It searches for Quail packages under `quail' subdirectory of DIRNAME,
  1846. and update the file \"leim-list.el\" in DIRNAME.
  1847.  
  1848. When called from a program, the remaining arguments are additional
  1849. directory names to search for Quail packages under `quail' subdirectory
  1850. of each directory."
  1851.   (interactive "FDirectory of LEIM: ")
  1852.   (setq dirname (expand-file-name dirname))
  1853.   (let ((leim-list (expand-file-name leim-list-file-name dirname))
  1854.     quail-dirs list-buf pkg-list pkg-buf pos)
  1855.     (if (not (file-writable-p leim-list))
  1856.     (error "Can't write to file \"%s\"" leim-list))
  1857.     (message "Updating %s ..." leim-list)
  1858.     (setq list-buf (find-file-noselect leim-list))
  1859.  
  1860.     ;; At first, clean up the file.
  1861.     (save-excursion
  1862.       (set-buffer list-buf)
  1863.       (goto-char 1)
  1864.  
  1865.       ;; Insert the correct header.
  1866.       (if (looking-at (regexp-quote leim-list-header))
  1867.       (goto-char (match-end 0))
  1868.     (insert leim-list-header))
  1869.       (setq pos (point))
  1870.       (if (not (re-search-forward leim-list-entry-regexp nil t))
  1871.       nil
  1872.  
  1873.     ;; Remove garbages after the header.
  1874.     (goto-char (match-beginning 0))
  1875.     (if (< pos (point))
  1876.         (delete-region pos (point)))
  1877.  
  1878.     ;; Remove all entries for Quail.
  1879.     (while (re-search-forward leim-list-entry-regexp nil 'move)
  1880.       (goto-char (match-beginning 0))
  1881.       (setq pos (point))
  1882.       (condition-case nil
  1883.           (let ((form (read list-buf)))
  1884.         (when (equal (nth 3 form) ''quail-use-package)
  1885.           (if (eolp) (forward-line 1))
  1886.           (delete-region pos (point))))
  1887.         (error
  1888.          ;; Delete the remaining contents because it seems that
  1889.          ;; this file is broken.
  1890.          (message "Garbages in %s deleted" leim-list)
  1891.          (delete-region pos (point-max)))))))
  1892.  
  1893.     ;; Search for `quail' subdirector under each DIRNAMES.
  1894.     (setq dirnames (cons dirname dirnames))
  1895.     (let ((l dirnames))
  1896.       (while l
  1897.     (setcar l (expand-file-name (car l)))
  1898.     (setq dirname (expand-file-name quail-directory-name (car l)))
  1899.     (if (file-readable-p dirname)
  1900.         (setq quail-dirs (cons dirname quail-dirs))
  1901.       (message "%s doesn't has `%s' subdirectory, just ignored"
  1902.            (car l) quail-directory-name)
  1903.       (setq quail-dirs (cons nil quail-dirs)))
  1904.     (setq l (cdr l)))
  1905.       (setq quail-dirs (nreverse quail-dirs)))
  1906.  
  1907.     ;; Insert input method registering forms.
  1908.     (while quail-dirs
  1909.       (setq dirname (car quail-dirs))
  1910.       (when dirname
  1911.     (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
  1912.     (while pkg-list
  1913.       (message "Checking %s ..." (car pkg-list))
  1914.       (with-temp-buffer
  1915.         (insert-file-contents (car pkg-list))
  1916.         (goto-char (point-min))
  1917.         (while (search-forward "(quail-define-package" nil t)
  1918.           (goto-char (match-beginning 0))
  1919.           (condition-case nil
  1920.           (let ((form (read (current-buffer))))
  1921.             (save-excursion
  1922.               (set-buffer list-buf)
  1923.               (insert
  1924.                (format "(register-input-method
  1925.  %S %S '%s
  1926.  %S %S
  1927.  %S)\n"
  1928.                    (nth 1 form) ; PACKAGE-NAME
  1929.                    (nth 2 form) ; LANGUAGE
  1930.                    'quail-use-package ; ACTIVATE-FUNC
  1931.                    (nth 3 form) ; PACKAGE-TITLE
  1932.                    (progn    ; PACKAGE-DESCRIPTION (one line)
  1933.                  (string-match ".*" (nth 5 form))
  1934.                  (match-string 0 (nth 5 form)))
  1935.                    (file-relative-name ; PACKAGE-FILENAME
  1936.                 (file-name-sans-extension (car pkg-list))
  1937.                 (car dirnames))))))
  1938.         (error
  1939.          ;; Ignore the remaining contents of this file.
  1940.          (goto-char (point-max))
  1941.          (message "Some part of \"%s\" is broken" dirname)))))
  1942.       (setq pkg-list (cdr pkg-list)))
  1943.     (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
  1944.  
  1945.     ;; At last, write out LEIM list file.
  1946.     (save-excursion
  1947.       (set-buffer list-buf)
  1948.       (setq buffer-file-coding-system 'iso-2022-7bit)
  1949.       (save-buffer 0))
  1950.     (kill-buffer list-buf)
  1951.     (message "Updating %s ... done" leim-list)))
  1952. ;;
  1953. (provide 'quail)
  1954.  
  1955. ;;; quail.el ends here
  1956.